package com.hxbd.test.utils;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Properties;

import org.testng.Assert;

import com.alibaba.fastjson.JSONObject;

public class GenerateTestCase { 

	public static void writeln (String filepath,String txt) {
		BufferedWriter bw  = null;
		try {
			bw = new BufferedWriter(new FileWriter(filepath, true));
			System.out.println(txt);
			bw.write(txt);
			bw.flush();
			bw.newLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void write (String filepath,String txt) {
		BufferedWriter bw  = null;
		try {
			bw = new BufferedWriter(new FileWriter(filepath, true));
			System.out.print(txt);
			bw.write(txt);
			bw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally {
			try {
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) {
		Properties pro = new Properties();
		InputStream in = GenerateTestCase.class.getClassLoader()
				.getResourceAsStream("params.properties");
		try {
			BufferedReader bf = new BufferedReader(new InputStreamReader(in, "utf-8"));
			pro.load(bf);
			in.close();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String[] a = pro.getProperty("necessaryStr").split(","); 
		String[] b = pro.getProperty("unNecessaryStr").split(","); 
		getTotalGroup(a,b);
	} 
	  
	
	private static void select(int k) { 
		String[] result = new String[k]; 
	   subselect(0, 1, result, k); 
	
	} 
	    
	public static void getTotalGroup (String [] necessaryStr, String [] unNecessaryStr) {
//		System.out.print("{}");
		write("D:/test.txt", "{}");
		writeln("D:/test.txt", "\t所有参数都不填");
//		System.out.println("\t所有参数都不填");
		a = necessaryStr;
		if (a == null || (a.length == 1 && a[0].isEmpty())) {
			
		}	
		else {
			for (int i = 1; i <= a.length; i++) {
				select(i);
			}
		}
		String tmpstr = test11(necessaryStr);
		if (unNecessaryStr == null || (unNecessaryStr.length == 1 && unNecessaryStr[0].isEmpty())) {
			
		}	
		else {
			String tmpstr2 = test11(unNecessaryStr);
			for (int j = 0; j <= unNecessaryStr.length - 1; j++) {
				if (tmpstr.equals("{}")) {
//					System.out.print("{"+unNecessaryStr[j]+"}");
					write("D:/test.txt", "{"+unNecessaryStr[j]+"}");
				}
				else {
//					System.out.print(tmpstr.substring(0, tmpstr.length()-1)+","+unNecessaryStr[j]+"}");
					write("D:/test.txt", tmpstr.substring(0, tmpstr.length()-1)+","+unNecessaryStr[j]+"}");
				}
				JSONObject json = JSONObject.parseObject("{"+unNecessaryStr[j]+"}");
//				System.out.println("\t必填参数全填，非必填参数传入:"+Arrays.toString(json.keySet().toArray()));
				writeln("D:/test.txt", "\t必填参数全填，非必填参数传入:"+Arrays.toString(json.keySet().toArray()));
			}
			
			JSONObject json2 = JSONObject.parseObject("{"+tmpstr2.substring(1, tmpstr2.length()));
//			System.out.print(tmpstr.substring(0, tmpstr.length()-1)+","+tmpstr2.substring(1, tmpstr2.length()));
//			System.out.println("\t必填参数全填，非必填参数传入:"+Arrays.toString(json2.keySet().toArray()));
			write("D:/test.txt", tmpstr.substring(0, tmpstr.length()-1)+","+tmpstr2.substring(1, tmpstr2.length()));
			writeln("D:/test.txt", "\t必填参数全填，非必填参数传入:"+Arrays.toString(json2.keySet().toArray()));
		}
	}
	    
	private static void subselect(int head, int index, String[] r, int k) { 
	   for (int i = head; i < a.length + index - k; i++) { 
	    if (index < k) { 
	     r[index - 1] = a[i]; 
	     subselect(i + 1, index + 1, r, k); 
	    } else if (index == k) { 
	     r[index - 1] = a[i];
	     String result = test11(r);
//	     System.out.print(result); 
	     write("D:/test.txt", result);
	     JSONObject json = JSONObject.parseObject(result);
//	     System.out.println("\t"+"必填参数中传入参数:"+Arrays.toString(json.keySet().toArray()));
	     writeln("D:/test.txt", "\t"+"必填参数中传入参数:"+Arrays.toString(json.keySet().toArray()));
	     subselect(i + 1, index + 1, r, k); 
	    } else { 
	    	return;
	    } 
	   } 
	} 
	public static String test11 (String []a) {
		String b = "";
		for(int i=0;i<a.length;i++){
			if (i == 0) {
				b+="{"+a[i];
			}
			else {
				b+=","+a[i];
			}	
		}
		b = b+"}";
		return b;
	}
	
	private static String[] a = null; 
}